package com.esheeper.CAS.Util;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@ControllerAdvice
@ResponseBody
public class ErrorUtil {

    @Autowired
    ResponseUtil responseUtil;

    @ExceptionHandler(value = Exception.class)// 拦截所有异常
    public ResponseEntity<Map> exceptionHandler(Exception e) {
        if (e instanceof org.springframework.dao.DuplicateKeyException) {
            return ResponseEntity.status(200).body( responseUtil.fail(ResponseUtil.REQUEST_ERROR_USERNAME_HAVE_EXIST,null) );
        } else {
            return ResponseEntity.status(200).body(responseUtil.fail(ResponseUtil.RESPONSE_ERROR_SER_ERROR, null));
        }
    }
}
